package org.nmgyjt.springboot.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.nmgyjt.springboot.beans.admin.*;
import org.nmgyjt.springboot.beans.dto.BusAuthUserAddDto;
import org.nmgyjt.springboot.beans.dto.UserAuthCodeParamDto;
import org.nmgyjt.springboot.beans.vo.CsBusinessListVo;
import org.nmgyjt.springboot.beans.vo.CsBusinessUserAuthListVo;
import org.nmgyjt.springboot.beans.vo.CsBusinessUserListVo;
import org.nmgyjt.springboot.entity.ApiRes;
import org.nmgyjt.springboot.entity.admin.CsBusiness;
import org.nmgyjt.springboot.service.IBusinessAuthService;
import org.nmgyjt.springboot.service.ICsBusinessService;
import org.nmgyjt.springboot.service.ICsBusinessUserAuthService;
import org.nmgyjt.springboot.service.ICsBusinessUserCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/cs/ba")
@CrossOrigin
@Slf4j
public class BusinessAuthController {

    @Autowired
    private IBusinessAuthService businessAuthService;

    /**
     * 添加用户权限
     * @param dto
     * @return
     */
    @PostMapping("/addAuthUserCode")
    public ApiRes addAuthUserCode(@RequestBody BusAuthUserAddDto dto,HttpServletRequest request) throws Exception {
        String ipAddress;
        // 如果存在X-Forwarded-For，则优先取该头信息中的IP地址
        String xForwardedFor = request.getHeader("X-Forwarded-For");
        if (xForwardedFor != null && !xForwardedFor.isEmpty()) {
            ipAddress = xForwardedFor.split(",")[0];
        } else {
            // 否则使用request.getRemoteAddr()作为备选方案
            ipAddress = request.getRemoteAddr();
        }
        log.info("Client IP address:{} " , ipAddress);
        dto.setIp(ipAddress);
        businessAuthService.addAuthUser(dto);
        return ApiRes.ok();
    }


    /**
     * 获取管理员和用户的系统权限
     * @param dto
     * @return
     */
    @PostMapping("/getBuAuthCodeList")
    public ApiRes getBuAuthCodeList(@RequestBody UserAuthCodeParamDto dto) {
        List<CsBusinessUserListVo>  list = businessAuthService.getBuAuthCodeList(dto);
        return ApiRes.ok(list);
    }


    /**
     * 获取用户业务系统list
     * @return
     */
    @GetMapping("/businessAllList")
    public ApiRes businessList() {
        List<CsBusinessListVo> listVo = new ArrayList<>();
        List<CsBusiness> list = businessAuthService.businessAllList();
       list.forEach(data->{
            CsBusinessListVo vo = new CsBusinessListVo();
            vo.setId(data.getBusinessId());
            vo.setName(data.getName());
            listVo.add(vo);
        });
        List<CsBusinessListVo> distinctStudents = listVo.stream()
                .distinct()
                .collect(Collectors.toList());
        return ApiRes.ok(distinctStudents);
    }

    @PostMapping("/listPage")
    public ApiRes listPage(@RequestBody CsBusinessUserAuthParamDTO bean) {
        IPage<CsBusinessUserAuthListVo> iPage = businessAuthService.listPage(bean);
        return ApiRes.ok(iPage);
    }

}
